/** 
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * 
 *PmProjectChangeController
 * 项目基本信息变更申请
 * 
 * 修改纪录
 * 2020-06-10 版本：1.0 liwanpeng 创建。
 * @version 版本：1.0
 * @author 作者：liwanpeng
 * 创建日期：2020-06-10
 */

package com.topscomm.pm.controller;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pm.controller.auto.PmProjectChangeControllerAuto;
import com.topscomm.pm.utils.MisStringUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author: liwanpeng
 * @date: 2020-06-10
 * @description:项目基本信息变更申请Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/projectChange")
public class PmProjectChangeController extends PmProjectChangeControllerAuto implements IPmProjectChangeController {
	/**
	 * @author: liwanpeng
	 * @date: 2020-06-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		// 项目名称
		if (paraMap.containsKey("qry_name2")) {
			String value = paraMap.get("qry_name2").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (name2 like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_projectmanagername2")) {
			String value = paraMap.get("qry_projectmanagername2").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (projectmanagername2 like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
		{
			String dataPession = service.getDataPermission(TapOperationTypeEnum.Select, "", this.getUserBean());
			if (StringUtil.isEmpty(dataPession)) {
				sql.appendSingle(" and (" + MisStringUtil.createWhereByUsercodeFromProject(
						this.getUserBean().getUser(), "projectid",
						service.getDao().getTableName()) +
								" or usercode = '{0}')",
						this.getUserBean().getUser().getCode());
			}  else// 使用数据权限
			{
				sql.appendSingle(" and {0} ", dataPession);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: liwanpeng
	 * @date: 2020-06-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * 
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: liwanpeng
	 * @date: 2020-06-10
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目基本信息变更申请申请,单据编号{3}! ", dataMap.get("dispdeptcode"),
				dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
		List<Map<String, Object>> flowidList = flowDatacodeCache.getApprovalFlowListMap("ApprovalFlow_PmProjectChange", dataMap.get("deptcode").toString(),dataMap.get("usercode").toString(),null, "sortcode");
		Long flowid = flowidList.size()>0? ConvertUtil.convertToLong(flowidList.get(0).get("id")) :null;
		dataMap.put("flowid", flowid);
	}

	/**
	 * 手机端审批load页面
	 * 
	 * @param paramMap
	 * @return
	 */
	@GetMapping("loadForMobile")
	public ResponseResult<Object> loadForMobile(@RequestParam(required = false) Map<String, Object> paramMap) {
		String changeId = ConvertUtil.convertToString(paramMap.get("id"));
		if (changeId == null || "".equals(changeId)) {
			return ResponseResult.error("项目基本信息变更申请单id不得为空");
		}
		// 生成手机端需要的数据格式
		Map<String, Object> mainListData = this.service.queryMapById(paramMap.get("id").toString());
		// 对比新值和原值，保留新值和原值不同的key
		Iterator<Entry<String, Object>> iterator = mainListData.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<String, Object> item = iterator.next();
			// 如果key 和 key+“2”都不为空则需要对比,如果值相同则不展示这个字段
			if (mainListData.get(item.getKey()) != null && mainListData.get(item.getKey() + "2") != null
					&& (ConvertUtil.convertToString(mainListData.get(item.getKey()))
							.equals(ConvertUtil.convertToString(mainListData.get(item.getKey() + "2"))))) {
				iterator.remove();
			} else if (item.getKey().contains("2")) {
				if (!mainListData.containsKey(item.getKey().substring(0, item.getKey().length() - 1))) {
					iterator.remove();
				}
			}else if (!item.getKey().contains("2")&& (!"id".equals(item.getKey())) &&!mainListData.containsKey(item.getKey()+"2")) {
				iterator.remove();
			}

		}
		// 将取出的详细数据放入
		List<Map<String, Object>> mainList = new ArrayList<>();
		mainList.add(mainListData);
		Map<String, Object> resultDataMap = new HashMap<>();
		resultDataMap.put("mainList", mainList);
		// 获取页面（约定：app管理页面配置中的页面编码对应controller表名）
		// appPageService.getPageInfo("project", resultDataMap);
		return ResponseResult.ok(mainListData);
	}
}
